package com.caigou.modules.channel.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.caigou.comm.base.ext.getScreenWidth
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.modules.channel.R
import com.caigou.modules.channel.bean.MedalItemBean

/**
 * @author #Suyghur.
 * Created on 2021/12/16
 */
class MedalDetailDialog(private val medalItemBean: MedalItemBean, context: Context) : Dialog(context) {

    private lateinit var ivIcon: ImageView
    private lateinit var tvName: TextView
    private lateinit var tvCondition: TextView
    private lateinit var tvReward: TextView
    private lateinit var tvGrowth: TextView
    private lateinit var btnCancel: Button
    lateinit var btnWear: Button
    lateinit var btnReceiver: Button

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView(context)
    }

    private fun initView(context: Context) {
        setContentView(R.layout.dialog_medal_detail)

        val attr = window?.attributes as WindowManager.LayoutParams
        //设置dialog在布局中的位置
        attr.gravity = Gravity.CENTER
        attr.width = getScreenWidth(context) - 20

        ivIcon = findViewById(R.id.medal_detail_iv_icon)
        ImageLoader.loadCircle(context, medalItemBean.medalIcon, ivIcon)

        tvName = findViewById(R.id.medal_detail_tv_icon_name)
        tvName.text = medalItemBean.medalName

        tvCondition = findViewById(R.id.medal_detail_tv_condition)
        tvCondition.text = medalItemBean.taskDesc

        tvReward = findViewById(R.id.medal_detail_tv_reward)
        tvReward.text = "奖励：${medalItemBean.medalAward}"

        tvGrowth = findViewById(R.id.medal_detail_tv_growth)
        tvGrowth.text = "成长值奖励：+${medalItemBean.growthValue}"

        btnCancel = findViewById(R.id.medal_detail_btn_cancel)
        btnCancel.setOnClickListener {
            dismiss()
        }

        btnWear = findViewById(R.id.medal_detail_btn_wear)
        when (medalItemBean.wearStatus) {
            0 -> {
                btnWear.text = "未佩戴"
                btnWear.isClickable = true
                btnWear.setBackgroundResource(R.drawable.comm_btn_light_blue)
            }
            1 -> {
                btnWear.text = "已佩戴"
                btnWear.isClickable = true
                btnWear.setBackgroundResource(R.drawable.comm_btn_light_blue)
            }
            2 -> {
                btnWear.text = "不可佩戴"
                btnWear.isClickable = false
                btnWear.setBackgroundResource(R.drawable.comm_btn_gray)
            }
        }

        btnReceiver = findViewById(R.id.medal_detail_btn_receive)
        when (medalItemBean.status) {
            0 -> {
                btnReceiver.text = "可领取"
                btnReceiver.isClickable = true
                btnReceiver.setBackgroundResource(R.drawable.comm_btn_light_blue)
            }
            1 -> {
                btnReceiver.text = "已领取"
                btnReceiver.isClickable = false
                btnReceiver.setBackgroundResource(R.drawable.comm_btn_gray)
            }
            2 -> {
                btnReceiver.text = "不可领取"
                btnReceiver.isClickable = false
                btnReceiver.setBackgroundResource(R.drawable.comm_btn_gray)
            }
        }
    }
}